package com.lecture.java8.excel;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.template.FillData;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class HeadTest {


    public static void main(String[] args) {
        FillData fillData = new FillData();
        fillData.setName("name");
        fillData.setDate(new Date());
        fillData.setNumber(12D);
        List<FillData> list = new ArrayList<>();
        list.add(fillData);

        List<List<String>> headList = new ArrayList<>();
        List<String> head0 = new ArrayList<>();
        head0.add("string" + System.currentTimeMillis());
        headList.add(head0);
        String fileName = "D:\\study\\tmp\\ab.xlsx";
        EasyExcel.write(fileName,FillData.class).head(headList).sheet("模板").doWrite(list);
    }
    public static void main3(String[] args) {
       try {
           /*HeadTest h = new HeadTest();
           h.test();*/
           List<String> list = Lists.newArrayList("三值","一值","五值","二值");
           System.out.println(list.toString());
           Collections.sort(list);
           System.out.println(list);
           Ordering<String> ordering = Ordering.explicit("一值", "二值", "三值", "四值", "五值","六值","七值");
           List<String> strings = ordering.sortedCopy(list);
           System.out.println(strings);
       } catch (Exception e) {
           e.printStackTrace();
       }
    }

    public void test() throws IOException {
        // 文件输出位置
        OutputStream out = new FileOutputStream("D:\\study\\tmp\\excel\\4.xlsx");/*
        ExcelWriter writer = EasyExcelFactory.write(out).build();

        // 动态添加表头，适用一些表头动态变化的场景
        WriteSheet sheet1 = new WriteSheet();
        sheet1.setSheetName("商品明细");
        sheet1.setSheetNo(0);
        // 创建一个表格，用于 Sheet 中使用
        WriteTable table = new WriteTable( );
        table.setTableNo(1);
        table.setHead(getHead());
        // 写数据
        writer.write(contentData(), sheet1, table);
        writer.finish();
        out.close();*/
        //需要合并的列
        int[] mergeColumnIndex = {0};
        // 从哪一行开始合并
        int mergeRowIndex = 1;
        /*EasyExcelFactory.write(out)
                .sheet(1)
                .head(getHead())
                .registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumnIndex))
                .doWrite(contentData());*/
    }

    private static List<List<String>> getHead() {
        List<List<String>> headTitles = Lists.newArrayList();
        String deviceType = "设备分类", nodeName = "所属节点", tagCode = "测点编码";
        headTitles.add(Lists.newArrayList(deviceType,deviceType,deviceType));
        headTitles.add(Lists.newArrayList(nodeName,nodeName,nodeName));
        headTitles.add(Lists.newArrayList(tagCode,tagCode,tagCode));

        List<String> dutyList = getDuty();
        dutyList.forEach(duty -> {
            headTitles.add(Lists.newArrayList(duty,"上限","次数"));
            headTitles.add(Lists.newArrayList(duty,"上限","最高温"));
            headTitles.add(Lists.newArrayList(duty,"下限","次数"));
            headTitles.add(Lists.newArrayList(duty,"下限","最高温"));
        });
        return headTitles;
    }

    public static List<String> getDuty() {
        return Lists.newArrayList("一值","二值","三值","四值","五值");
    }

    private static List <List<String>> head(){
        List<List<String>> headTitles = Lists.newArrayList();
        String basicInfo = "基础资料",skuInfo = "商品扩展",orderInfo = "经营情况",empty = " ";
        //第一列，1/2/3行
        headTitles.add( Lists.newArrayList(basicInfo ,basicInfo,"类别") );
        //第二列，1/2/3行
        headTitles.add( Lists.newArrayList(basicInfo,basicInfo,"名称" ) );

        List<String>  skuTitles = Lists.newArrayList("组合商品", "上一次优惠时间", "销售次数", "库存", "价格");
        skuTitles.forEach(title->{
            headTitles.add( Lists.newArrayList(skuInfo ,skuInfo,title) );
        });
        List<Integer> monthList = Lists.newArrayList(5,6);
        //动态根据月份生成
        List<String> orderSpeaces = Lists.newArrayList("销售额", "客流", "利润");
        monthList.forEach(month->{
            orderSpeaces.forEach(title->{
                headTitles.add( Lists.newArrayList(orderInfo ,  month+"月" ,title ) );
            });
        });
        //无一、二行标题
        List<String> lastList = Lists.newArrayList("日均销售金额(元)", "月均销售金额(元)" );
        lastList.forEach(title->{
            headTitles.add( Lists.newArrayList(empty , empty ,title ) );
        });
        return headTitles;
    }

    private static List<List<Object>> contentData(){
        List<List<Object>> contentList = Lists.newArrayList();
        //这里一个List<Object>才代表一行数据，需要映射成每行数据填充，横向填充（把实体数据的字段设置成一个List<Object>）
        contentList.add( Lists.newArrayList("测试", "商品A","苹果🍎","","k") );
        contentList.add( Lists.newArrayList("测试", "商品B","橙子🍊") );
        return contentList;
    }


}
